/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * InjectMemberJDialog.java
 *
 * Created on Jan 4, 2010, 10:19:06 PM
 */
package jaspect;

import jaspect.model.AspectConfiguration;
import jaspect.model.AspectMethod;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Ajin Baby
 */
public class InjectMemberJDialog extends javax.swing.JDialog {

    /** Creates new form InjectMemberJDialog
     * @param parent
     * @param modal
     */
    public InjectMemberJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        for (int i = 0; i < tableMethods.getRowCount(); i++) {
            tableMethods.setValueAt("", i, 0);
            tableMethods.setValueAt(false, i, 1);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonInject = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableMethods = new javax.swing.JTable();
        labelAspectName = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        buttonInject.setText("Inject");
        buttonInject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonInjectActionPerformed(evt);
            }
        });

        tableMethods.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "Method", "Inject"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tableMethods);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(253, 253, 253)
                        .addComponent(buttonInject))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 485, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(labelAspectName, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(169, 169, 169)
                        .addComponent(labelAspectName, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(50, 50, 50)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 25, Short.MAX_VALUE)
                .addComponent(buttonInject)
                .addGap(38, 38, 38))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     *
     * @param ac
     */
    public void setParent(Main ac) {
        parent = ac;

    }

    @Override
    public Main getParent() {
        return parent;

    }
    private void buttonInjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonInjectActionPerformed
        // TODO add your handling code here:

        List<AspectMethod> al = new ArrayList<AspectMethod>();
        try {
            for (int i = 0; i < tableMethods.getRowCount(); i++) {
                if (tableMethods.getValueAt(i, 0) == "" || tableMethods.getValueAt(i, 1) == "") {
                    break;
                }
                AspectMethod a = (AspectMethod) tableMethods.getValueAt(i, 0);
                if (tableMethods.getValueAt(i, 1).equals(true)) {
                    al.add(a);
                }

            }
        } catch (Exception e) {
            System.out.println("InjectMemberTableRead " + e);
        }


        StringBuilder sb = new StringBuilder();
        sb.append(parent.getEditorJava().getText());
        int j = sb.lastIndexOf("}");
        sb.delete(j, j + 1);
        int start = 0;
        int end = 0;
        String aspectName;
        while(true)
        {
            start = sb.indexOf("#",end+1 );
            if(start==-1)
                break;
            end = sb.indexOf("#",start+1);
            if(end==-1)
                break;//miss match in #
            aspectName =sb.substring(start+1,sb.indexOf("\n",start));
            if(aspectName.equals(this.aspc.getName()))
            {
                sb.delete(start,end+1);
                break;
            }
        }
        sb.append(aspc.toAspectString(al));
        sb.append("\n}");
        parent.getEditorJava().setText(sb.toString());
        this.setVisible(false);


    }//GEN-LAST:event_buttonInjectActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                InjectMemberJDialog dialog = new InjectMemberJDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    /**
     *
     * @param tp
     */
    public void displayMethods(AspectConfiguration tp) {
        int j = 0;
        for (AspectMethod i : tp.getMethods().values()) {
            tableMethods.setValueAt(i, j, 0);
            j++;
        }
    }
    private Main parent;
    private AspectConfiguration aspc;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonInject;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelAspectName;
    private javax.swing.JTable tableMethods;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the aspc
     */
    public AspectConfiguration getAspc() {
        return aspc;
    }

    /**
     * @param aspc the aspc to set
     */
    public void setAspc(AspectConfiguration aspc) {
        this.aspc = aspc;
    }
}
